package com.zhu.responder.controller;


import com.alibaba.fastjson.JSON;
import com.zhu.responder.pojo.R;

import com.zhu.responder.websocker.handler.ChannelPool;

import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author ZhuTong
 * @description 抢答的处理器
 * @create 2021-01-22 15:41
 */
@RestController
@CrossOrigin
public class ResponderController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/compere/frequency")
    public R compereFrequency(){
        String frequency = UUID.randomUUID().toString().replaceAll("-", "");
        //redisTemplate.opsForValue().set("frequencyStutas", 1);
        HashMap<String, Object> map = new HashMap<>();
        map.put("frequency", frequency);
        map.put("frequencyStutas", 1);
        TextWebSocketFrame textWebSocketFrame = new TextWebSocketFrame(JSON.toJSONString(map));
        ChannelPool.channels.writeAndFlush(textWebSocketFrame);
        redisTemplate.opsForValue().set(frequency+"Time",new Date().getTime());
        new Thread(()->{
            try {
                redisTemplate.opsForValue().set("frequency", frequency);
                Thread.sleep(10000);
                map.put("frequencyStutas", 0);
                TextWebSocketFrame textWebSocketFrame1 = new TextWebSocketFrame(JSON.toJSONString(map));
                ChannelPool.channels.writeAndFlush(textWebSocketFrame1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();

        return R.ok(frequency);
    }


    @PostMapping("/reciprocator/click")
    public R reciprocatorList(@RequestBody Map<String,String> map) {
        Date now = new Date();
        String frequency = map.get("frequency");
        String name = map.get("name");
        Double score = redisTemplate.opsForZSet().score("list:" + frequency, name);
        if (score!=null){
            return R.ok("别搞我，你别重复点击了");
        }
        long date = (long)redisTemplate.opsForValue().get(frequency+"Time");
        long time = now.getTime()-date;
        redisTemplate.opsForZSet().add("list:"+frequency, name, time);
        return R.ok();
    }


    @GetMapping("/reciprocator/list")
    public R reciprocatorList() {
        String frequency = redisTemplate.opsForValue().get("frequency").toString();
        Set<ZSetOperations.TypedTuple<String>> set = redisTemplate.opsForZSet().rangeWithScores("list:" + frequency, 0, -1);
        return R.ok(set);
    }

    @GetMapping("/houmen")
    public R houmen(@RequestParam String name,@RequestParam int ms) {
        System.out.println(name);
        String frequency = redisTemplate.opsForValue().get("frequency").toString();
        redisTemplate.opsForZSet().add("list:"+frequency, name, ms);
        return R.ok();
    }


}
